package com.yuanli.cosmeticmirror.model.impl;

import android.util.Log;

import androidx.lifecycle.LifecycleOwner;

import com.alibaba.fastjson.JSONObject;
import com.comenjoysoft.baselibrary.util.AppUtils;
import com.comenjoysoft.baselibrary.util.DeviceUtils;
import com.comenjoysoft.baselibrary.util.RomUtils;
import com.comenjoysoft.baselibrary.util.SPUtils;
import com.orhanobut.logger.Logger;
import com.rxjava.rxlife.BaseScope;
import com.rxjava.rxlife.RxLife;
import com.yuanli.cosmeticmirror.base.Constant;
import com.yuanli.cosmeticmirror.base.MyApp;
import com.yuanli.cosmeticmirror.model.biz.IWelComeBiz;
import com.yuanli.cosmeticmirror.utils.PhoneUtils;


import java.util.HashMap;

import rxhttp.wrapper.param.RxHttp;

import static com.yuanli.cosmeticmirror.base.HttpApi.GET_APP_ACTIVATIONAMOUNT;
import static com.yuanli.cosmeticmirror.base.HttpApi.GET_FIRSTRUNAPPNEW;
import static com.yuanli.cosmeticmirror.base.HttpApi.GET_FIRST_OPEN;
import static com.yuanli.cosmeticmirror.base.HttpApi.GET_STATISTICS_DAILY;
import static com.yuanli.cosmeticmirror.base.MyApp.TAG;

public class WelcomeImpl extends BaseScope implements IWelComeBiz {


    public WelcomeImpl(LifecycleOwner owner) {
        super(owner);
    }

    @Override
    public void firstRunApp() {
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("appName", "化妆镜");
        hashMap.put("Client_type", RomUtils.getRomInfo().getName());
        hashMap.put("pid", PhoneUtils.getDeviceId(MyApp.getInstance()));
        JSONObject json = new JSONObject(hashMap);
        Log.d(TAG, "firstRunApp json " + json.toString() + "      ----->url" + GET_APP_ACTIVATIONAMOUNT);
        RxHttp.get(GET_APP_ACTIVATIONAMOUNT)
                .addAll(hashMap)//发送Json字符串单形式的Post请求
                .asString()
                .as(RxLife.asOnMain(this)) //返回String类型
                .subscribe(s -> {          //订阅观察者，
                    Logger.d(" firstRunApp onSuccess: " + s);
                }, throwable -> {
                    Logger.d(" firstRunApp onFailed: " + throwable.getMessage());
                });
    }


    @Override
    public void firstRunAppNew() {
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("AppName", "化妆镜");
        hashMap.put("StoreName", "Xiaomi");
        hashMap.put("Version", "v" + AppUtils.getAppVersionName(MyApp.getInstance()));
        RxHttp.get(GET_FIRSTRUNAPPNEW)
                .addAll(hashMap)
                .asString()
                .as(RxLife.asOnMain(this)) //返回String类型
                .subscribe(s -> {          //订阅观察者，
                    Logger.d(" firstRunAppNew onSuccess: " + s);
                }, throwable -> {
                    Logger.d(" firstRunAppNew onFailed: " + throwable.getMessage());
                });
    }

    @Override
    public void firsLogin() {
        RxHttp.get(GET_STATISTICS_DAILY)
                .add("appname", "化妆镜")
                .asString()
                .as(RxLife.asOnMain(this)) //返回String类型
                .subscribe(s -> {          //订阅观察者，
                    Logger.d(" firsLogin onSuccess: " + s);
                }, throwable -> {
                    Logger.d(" firsLogin onFailed: " + throwable.getMessage());
                });
    }

    @Override
    public void firsOpen() {
        RxHttp.get(GET_FIRST_OPEN)
                .add("appname", "化妆镜")
                .add("acount", SPUtils.getInstance().getString(Constant.SP_USER_ACCOUNT))
                .asString()
                .as(RxLife.asOnMain(this)) //返回String类型
                .subscribe(s -> {          //订阅观察者，
                    Logger.d(" firsOpen onSuccess: " + s);
                }, throwable -> {
                    Logger.d(" firsOpen onFailed: " + throwable.getMessage());
                });
    }
}
